/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
* This is the definition file for the GIMI message:
* GIMI_PROTOCOL_AVANT_REPLY_V3
* 
*/
#ifndef _AVANT_REPLY_HEADER_H_
#define _AVANT_REPLY_HEADER_H_
#include <inttypes.h>
#include <assert.h>
#include "crc16.h"
#include "owndebug.h"

#define TGIMI_PROTOCOL_AVANT_REPLY_V3_SIZE 8 ///< Size of this message

struct TGIMI_PROTOCOL_AVANT_REPLY_V3
{
public:
  TGIMI_PROTOCOL_AVANT_REPLY_V3()
    : motor_rpm(0),
      left_speed(0),
      right_speed(0),
      input_pressure(0),
      load_pressure(0),
      digitalGear(0),
      digitalDoublePump(0),
      digitalLights(0),
      digitalBreaks(0),
      digitalTelescope(0),
      digitalRemoteCtrl(0),
      index(0),
      crc_low(0)
  {}

  ~TGIMI_PROTOCOL_AVANT_REPLY_V3()
  {}

  static unsigned short CreateCRC(unsigned char *data, int uLen)
  { 
    return crc16((const char *)data,(int)uLen);
  } 
  
  void Print(const int aLvl) const
  {
    dPrint(aLvl, 
	   "TeleProtocol_Reply_v3:\r\n"
	   "\tMotor_rpm: %d (%.2f rpm)\r\n"
	   "\tLeftSpeed: %d (%.2f km/h)\r\n"
	   "\tRightSpeed: %d (%.2f km/h)\r\n"
	   "\tI_pressure: %d (%.2f bar)\r\n"
	   "\tL_pressure: %d (%.2f bar)\r\n"
	   "\tFlag: Remote: %d, TeleS: %d, Breaks: %d, Lights: %d, Pumps: %d, Gear: %d\r\n"
	   "\tIndex: %d\r\n"
	   "\tCRC: %02x",
	   motor_rpm, 1.0*(4000.0/254.0)*(motor_rpm+127),
	   left_speed, 1.0*(15.0/127.0)*left_speed,
	   right_speed, 1.0*(15.0/127.0)*right_speed,
	   input_pressure, 1.0*(input_pressure+127),
	   load_pressure, 1.0*(load_pressure+127),
	   digitalRemoteCtrl,
	   digitalTelescope,
	   digitalBreaks,
	   digitalLights,
	   digitalDoublePump,
	   digitalGear,
	   index,
	   crc_low);
  }

  bool CheckCRC(void)
  {
    unsigned short int crc = CreateCRC((unsigned char *)this,
				       sizeof(TGIMI_PROTOCOL_AVANT_REPLY_V3)-1);
    return (crc == crc_low);
  }
  
public:
  char motor_rpm;                  ///< Motor RPM (0 - 4000)
  char left_speed;                 ///< -15km/h, 0, +15km/h
  char right_speed;                ///< -15km/h, 0, +15km/h
  char input_pressure;             ///< Pressure; 0 - 255 BAR
  char load_pressure;              ///< Pressure; 0 - 255 BAR
  unsigned digitalGear        : 2; ///< Gear; 0:free, 1:fw, 2:bw
  unsigned digitalDoublePump  : 1; ///< Two pumps enabled, otherwise one
  unsigned digitalLights      : 1; ///< Light on 
  unsigned digitalBreaks      : 1; ///< Breaks on (1)
  unsigned digitalTelescope   : 2; ///< Telescope; 0:stop, 1:in, 2:out
  unsigned digitalRemoteCtrl  : 1; ///< Remote control?
  char index;                      ///< Index of packet (currently unused) 
  unsigned char crc_low;                    ///< Low byte of CCIT-CRC
};


#endif
